Option Strict Off
Option Explicit On
Module DirectShowPlayback
	'This module handles all the setting up of the playback window and also the editor window, and also its removal when required.
	
	
	Public PlayBackVideoWindow As QuartzTypeLib.IVideoWindow 'The main playback window media player
	Public PlayBackMediaControl As QuartzTypeLib.IMediaControl
	Public PlayBackMediaPosition As QuartzTypeLib.IMediaPosition
	
	Public EditorVideoWindow As QuartzTypeLib.IVideoWindow 'The editor window media player
	Public EditorMediaControl As QuartzTypeLib.IMediaControl
	Public EditorMediaPosition As QuartzTypeLib.IMediaPosition
	
	Public Function OpenDShowPlayBack(ByRef PlaybackFilename As String, ByRef PicBox As System.Windows.Forms.PictureBox) As Boolean 'Open the Playback media player. NOTE THIS HAS NO SOUND CAPABILITY JUST VIDEO
		OpenDShowPlayBack = False 'Set function to initially return an error
		
		Call RemoveDShowPlayBack() 'Clean up any old player
		
		On Error GoTo NoCodec
		PlayBackMediaControl = New QuartzTypeLib.FilgraphManager 'Create the new empty filter graph
		Call GenerateFilterGraph(PlayBackMediaControl, PlaybackFilename)
		
		PlayBackVideoWindow = PlayBackMediaControl 'Attach the media control to the video window
		PlayBackVideoWindow.WindowStyle = 0 'Set the window style to none.
		PlayBackVideoWindow.Top = 0 'Ensure the video window is in the right place over the picturebox
		PlayBackVideoWindow.Left = 0
		PlayBackVideoWindow.Width = VB6.PixelsToTwipsX(PicBox.Width)
		PlayBackVideoWindow.Height = VB6.PixelsToTwipsY(PicBox.Height)
		PlayBackVideoWindow.Owner = PicBox.Handle.ToInt32 'Set the target picturebox
		
		PlayBackMediaPosition = PlayBackMediaControl 'Enable seeking etc.
		PlayBackMediaControl.Run() 'Play and pause video so first frame shows in window
		PlayBackMediaControl.Pause()
		PlayBackMediaPosition.CurrentPosition = PlayBackMediaPosition.Duration 'Move video to last frame.
		OpenDShowPlayBack = True 'Set function to return ok
		Exit Function
NoCodec: 
		If PlaybackFilename <> "" Then 'just end if no file has been chosen, but if there are any other errors tell user
			Call MsgBox("I cannot open the .avi file for playback", MsgBoxStyle.OKOnly + MsgBoxStyle.Exclamation, "Helium Frog Animator")
		End If
	End Function
	
	Public Function OpenDShowEditor(ByRef PlaybackFilename As String, ByRef PicBox As System.Windows.Forms.PictureBox) As Object 'Open the Editor media player
		'UPGRADE_WARNING: Couldn't resolve default property of object OpenDShowEditor. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
		OpenDShowEditor = False 'Set function to initially return an error
		
		Call RemoveDShowEditor() 'Clean up any old player
		
		On Error GoTo NoCodec
		EditorMediaControl = New QuartzTypeLib.FilgraphManager 'Create the new empty filter graph
		Call GenerateFilterGraph(EditorMediaControl, PlaybackFilename) 'Generate the complete filter graph
		
		EditorVideoWindow = EditorMediaControl 'Attach the media control to the video window
		EditorVideoWindow.WindowStyle = 0 'Set the window style to none.
		EditorVideoWindow.Top = 0 'Ensure the video window is in the right place over the picturebox
		EditorVideoWindow.Left = 0
		EditorVideoWindow.Width = VB6.PixelsToTwipsX(PicBox.Width)
		EditorVideoWindow.Height = VB6.PixelsToTwipsY(PicBox.Height)
		EditorVideoWindow.Owner = PicBox.Handle.ToInt32 'Set the target picturebox
		
		EditorMediaPosition = EditorMediaControl 'Enable seeking etc.
		EditorMediaControl.Run() 'Play and pause video so first frame shows in window
		EditorMediaControl.Pause()
		EditorMediaPosition.CurrentPosition = EditorMediaPosition.Duration 'Move video to last frame.
		'UPGRADE_WARNING: Couldn't resolve default property of object OpenDShowEditor. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
		OpenDShowEditor = True 'Set function to return ok
		Exit Function
NoCodec: 
		'UPGRADE_WARNING: Dir has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
		If Dir(PlaybackFilename) <> "" Then 'just end if no file has been chosen, but if there are any other errors tell user
			Call MsgBox("I cannot find the playback file", MsgBoxStyle.OKOnly + MsgBoxStyle.Exclamation, "Helium Frog Animator")
			Exit Function
		End If
	End Function
	Private Sub GenerateFilterGraph(ByRef FilterGraphName As QuartzTypeLib.FilgraphManager, ByRef SourceFile As String)
		'Supplied with an empty filtergraph name and the name of the source file, this routine manually generates the filter graph and connects pins.
		
		
		'Start of filter graph setup
		
		Dim FilterCount As Integer
		FilterCount = 0
		
		
		Dim g_objregfilters As Object
		g_objregfilters = FilterGraphName.RegFilterCollection 'Set the variable to contain a filter list, ie all the filters available on the system
		
		Dim FilterData As QuartzTypeLib.IRegFilterInfo 'Variable to hold the information about the filter
		
		'These variables hold the "Handles" to each filter. We need to obtain their values so we can investigate the pin IDs later.
		Dim AVISplitID As QuartzTypeLib.IFilterInfo
		Dim MJPEGDecID As QuartzTypeLib.IFilterInfo
		Dim ColSpaceConvID As QuartzTypeLib.IFilterInfo
		Dim VideoRendID As QuartzTypeLib.IFilterInfo
		Dim SourceFilterID As QuartzTypeLib.IFilterInfo
		
		FilterGraphName.AddSourceFilter(SourceFile, SourceFilterID) 'Add the source filter to the filter graph, returns the SourceFilterID identifier
		
		For	Each FilterData In g_objregfilters 'Go through and find the filter IDs for the filters we need.
			If FilterData.Name = "AVI Splitter" Then
				FilterData.filter(AVISplitID)
				FilterCount = FilterCount + 1
			End If
			If FilterData.Name = "MJPEG Decompressor" Then
				FilterData.filter(MJPEGDecID)
				FilterCount = FilterCount + 1
			End If
			If FilterData.Name = "Color Space Converter" Then
				FilterData.filter(ColSpaceConvID)
				FilterCount = FilterCount + 1
			End If
			If FilterData.Name = "Video Renderer" Then
				FilterData.filter(VideoRendID)
				FilterCount = FilterCount + 1
			End If
		Next FilterData
		If FilterCount < 4 Then GoTo NoCodec
		
		'These variables hold the "Handles" to each pin. We need to obtain their values so we can connect the pins.
		Dim SourcePinOut As QuartzTypeLib.IPinInfo
		Dim AVIPinIn As QuartzTypeLib.IPinInfo
		Dim AVIPinOut As QuartzTypeLib.IPinInfo
		Dim MJPEGPinIn As QuartzTypeLib.IPinInfo
		Dim MJPEGPinOut As QuartzTypeLib.IPinInfo
		Dim ColSpaceConvPinIn As QuartzTypeLib.IPinInfo
		Dim ColSpaceConvPinOut As QuartzTypeLib.IPinInfo
		Dim VideoRendererPinIn As QuartzTypeLib.IPinInfo
		
		'Find output pin on sourcefilter
		For	Each SourcePinOut In SourceFilterID.Pins
			If SourcePinOut.Direction = 1 Then Exit For 'We have found the pin , so exit the for next loop. The SourcePinOut variable now holds the ID of the pin
		Next SourcePinOut
		
		'Find the input pin on the AVI Splitter
		For	Each AVIPinIn In AVISplitID.Pins
			If AVIPinIn.Direction = 0 Then Exit For 'We have found the pin , so exit the for next loop. The AVIPinIn variable now holds the ID of the pin
		Next AVIPinIn
		
		SourcePinOut.ConnectDirect(AVIPinIn) 'Connect the two pins
		
		'Find output pin on AVI Splitter
		For	Each AVIPinOut In AVISplitID.Pins
			If AVIPinOut.Direction = 1 Then Exit For 'We have found the pin , so exit the for next loop. The AVIPinOut variable now holds the ID of the pin
		Next AVIPinOut
		
		'Find the input pin on the MJPEG Decompressor
		For	Each MJPEGPinIn In MJPEGDecID.Pins
			If MJPEGPinIn.Direction = 0 Then Exit For 'We have found the pin , so exit the for next loop. The MJPEGinIn variable now holds the ID of the pin
		Next MJPEGPinIn
		
		
		AVIPinOut.ConnectDirect(MJPEGPinIn) 'Connect the two pins
		
		'Find output pin on the MJPEG Decompressor
		For	Each MJPEGPinOut In MJPEGDecID.Pins
			If MJPEGPinOut.Direction = 1 Then Exit For 'We have found the pin , so exit the for next loop. The MJPEGPinOut variable now holds the ID of the pin
		Next MJPEGPinOut
		
		'Find the input pin on the Color Space Converter
		For	Each ColSpaceConvPinIn In ColSpaceConvID.Pins
			If ColSpaceConvPinIn.Direction = 0 Then Exit For 'We have found the pin , so exit the for next loop. The ColSpaceConvPinIn variable now holds the ID of the pin
		Next ColSpaceConvPinIn
		
		MJPEGPinOut.ConnectDirect(ColSpaceConvPinIn) 'Connect the two pins
		
		'Find output pin on the Color Space Converter
		For	Each ColSpaceConvPinOut In ColSpaceConvID.Pins
			If ColSpaceConvPinOut.Direction = 1 Then Exit For 'We have found the pin , so exit the for next loop. The ColSpaceConvPinOut variable now holds the ID of the pin
		Next ColSpaceConvPinOut
		
		'Find the input pin on the Video Renderer
		For	Each VideoRendererPinIn In VideoRendID.Pins
			If VideoRendererPinIn.Direction = 0 Then Exit For 'We have found the pin , so exit the for next loop. The VideoRendererPinIn variable now holds the ID of the pin
		Next VideoRendererPinIn
		
		ColSpaceConvPinOut.ConnectDirect(VideoRendererPinIn) 'Connect the two pins
		
		'End of filter graph setup
		Exit Sub
NoCodec: 
		If FilterCount < 4 Then
			ErrorCodeNo = "02.06.ds"
			ErrorDescription = "I cannot find the DirectShow filters to enable .avi playback" : Call frmErrorBox.ErrorMessageBox() : Call ExitProgram()
		End If
		
	End Sub
	Public Sub RemoveDShowPlayBack() 'clean up PlayBack media player and variables
		
		On Error GoTo RemoveDShowError
		
		'UPGRADE_ISSUE: ObjPtr function is not supported. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="367764E5-F3F8-4E43-AC3E-7FE0B5E074E2"'
        If Not PlayBackMediaControl Is Nothing Then PlayBackMediaControl.Stop() 'If a MediaControl instance exists, then stop it from playing
		
		'Destroy all objects
		'UPGRADE_ISSUE: ObjPtr function is not supported. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="367764E5-F3F8-4E43-AC3E-7FE0B5E074E2"'
		'UPGRADE_NOTE: Object PlayBackMediaControl may not be destroyed until it is garbage collected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6E35BFF6-CD74-4B09-9689-3E1A43DF8969"'
        If Not PlayBackMediaControl Is Nothing Then PlayBackMediaControl = Nothing
		'UPGRADE_ISSUE: ObjPtr function is not supported. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="367764E5-F3F8-4E43-AC3E-7FE0B5E074E2"'
		'UPGRADE_NOTE: Object PlayBackVideoWindow may not be destroyed until it is garbage collected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6E35BFF6-CD74-4B09-9689-3E1A43DF8969"'
        If Not PlayBackVideoWindow Is Nothing Then PlayBackVideoWindow = Nothing
		'UPGRADE_ISSUE: ObjPtr function is not supported. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="367764E5-F3F8-4E43-AC3E-7FE0B5E074E2"'
		'UPGRADE_NOTE: Object PlayBackMediaPosition may not be destroyed until it is garbage collected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6E35BFF6-CD74-4B09-9689-3E1A43DF8969"'
        If Not PlayBackMediaPosition Is Nothing Then PlayBackMediaPosition = Nothing
		Exit Sub
		
RemoveDShowError: 
		Err.Clear()
		PlayBackMediaControl.RenderFile((My.Application.Info.DirectoryPath & "\NonExistentFile.avi")) 'point the media player to a non existent .avi file. If this is not done, the .avi file is not freed so that
		
	End Sub
	
	Public Sub RemoveDShowEditor() 'clean up Editor media player and variables.(This is public as it is used in the exit routine)
		
		On Error GoTo RemoveDShowError
		
		'UPGRADE_ISSUE: ObjPtr function is not supported. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="367764E5-F3F8-4E43-AC3E-7FE0B5E074E2"'
        If Not EditorMediaControl Is Nothing Then EditorMediaControl.Stop() 'If a MediaControl instance exists, then stop it from playing
		
		'Destroy all objects
		'UPGRADE_ISSUE: ObjPtr function is not supported. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="367764E5-F3F8-4E43-AC3E-7FE0B5E074E2"'
		'UPGRADE_NOTE: Object EditorMediaControl may not be destroyed until it is garbage collected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6E35BFF6-CD74-4B09-9689-3E1A43DF8969"'
        If Not EditorMediaControl Is Nothing Then EditorMediaControl = Nothing
		'UPGRADE_ISSUE: ObjPtr function is not supported. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="367764E5-F3F8-4E43-AC3E-7FE0B5E074E2"'
		'UPGRADE_NOTE: Object EditorVideoWindow may not be destroyed until it is garbage collected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6E35BFF6-CD74-4B09-9689-3E1A43DF8969"'
        If Not EditorVideoWindow Is Nothing Then EditorVideoWindow = Nothing
		'UPGRADE_ISSUE: ObjPtr function is not supported. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="367764E5-F3F8-4E43-AC3E-7FE0B5E074E2"'
		'UPGRADE_NOTE: Object EditorMediaPosition may not be destroyed until it is garbage collected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6E35BFF6-CD74-4B09-9689-3E1A43DF8969"'
        If Not EditorMediaPosition Is Nothing Then EditorMediaPosition = Nothing
		Exit Sub
RemoveDShowError: 
		Err.Clear()
		EditorMediaControl.RenderFile((My.Application.Info.DirectoryPath & "\NonExistentFile.avi")) 'point the media player to a non existent .avi file. If this is not done, the .avi file is not freed so that
		frmEditor.txtRapidText.Text = ReportNotReady 'Make sure the media player cannot be played again without a rebuild (change button caption)
		Call frmEditor.RedButton() 'Turn button text to red
	End Sub
End Module